package userInterface.Panels.CommandArgumentPanels;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.*;
import javax.swing.event.*;

/**
 * {@link userInterface.panels.CommandArgumentPanels.PanelCommandArguments PanelCommandArguments} for editing
 * {@link settings.Action.Action#KEYSTROKE Action.KEYSTROKE} {@link settings.Command Command} arguments.
 * @author BDS
 */
public class PanelKeystrokeArguments extends PanelCommandArguments
{
	private static final long serialVersionUID = 1L;
	
	private GridBagLayout gblKeystrokeArguments;
	
	private JLabel lblKey1;
    private JComboBox<String> cbxKey1;
	private JLabel lblKey2;
	private JComboBox<String> cbxKey2;

	/**
	 * Class constructor.
	 */
	public PanelKeystrokeArguments()
	{
		gblKeystrokeArguments = new GridBagLayout();
		gblKeystrokeArguments.columnWidths = new int[] { 100, 0 } ;
		gblKeystrokeArguments.columnWeights = new double[] { 0.0, 1.0 };
		setLayout(gblKeystrokeArguments);

		lblKey1 = new JLabel("Key:");
        GridBagConstraints gbc_lblKey1 = new GridBagConstraints();
        gbc_lblKey1.anchor = GridBagConstraints.WEST;
        gbc_lblKey1.insets = new Insets(0, 0, 5, 5);
        gbc_lblKey1.gridx = 0;
        gbc_lblKey1.gridy = 0;
        add(lblKey1, gbc_lblKey1);

        cbxKey1 = new JComboBox<String>(getKeyCommands());
        GridBagConstraints gbc_cbxKey1 = new GridBagConstraints();
        gbc_cbxKey1.fill = GridBagConstraints.HORIZONTAL;
        gbc_cbxKey1.insets = new Insets(0, 0, 5, 0);
        gbc_cbxKey1.gridx = 1;
        gbc_cbxKey1.gridy = 0;
        add(cbxKey1, gbc_cbxKey1);
		
		lblKey2 = new JLabel("Key:");
        GridBagConstraints gbc_lblKey2 = new GridBagConstraints();
        gbc_lblKey2.anchor = GridBagConstraints.WEST;
        gbc_lblKey2.insets = new Insets(0, 0, 0, 5);
        gbc_lblKey2.gridx = 0;
        gbc_lblKey2.gridy = 1;
        add(lblKey2, gbc_lblKey2);

		cbxKey2 = new JComboBox<String>(getKeyCommands());
        GridBagConstraints gbc_cbxKey2 = new GridBagConstraints();
        gbc_cbxKey2.fill = GridBagConstraints.HORIZONTAL;
        gbc_cbxKey2.gridx = 1;
        gbc_cbxKey2.gridy = 1;
        add(cbxKey2, gbc_cbxKey2);
	}
	
	/**
     * Sets the enabled state of contained controls.
     * @param enabled Determines the enabled state of contained controls.
     */
	@Override
	public void setEnabled(boolean enabled)
	{
        super.setEnabled(enabled);
        
		lblKey1.setEnabled(enabled);
        cbxKey1.setEnabled(enabled);
        
		lblKey2.setEnabled(enabled);
		cbxKey2.setEnabled(enabled);
	}
	
    /**
     * Provides the external event processing class references to the contained {@link javax.swing.JComponent JComponents}.
     * @param actionListener The {@link java.awt.event.ActionListener ActionListener} for {@link javax.swing.JComboBox JComboBoxes}.
     * @param focusAdapter The {@link java.awt.event.FocusAdapter FocusAdapter} for {@link javax.swing.JFormattedTextField JFormattedTextFields}.
     * @param keyAdapter The {@link java.awt.event.KeyAdapater KeyAdapater} for {@link javax.swing.JFormattedTextField JFormattedTextFields}.
     * @param changeListener The {@link javax.swing.event.ChangeListener ChangeListener} for {@link javax.swing.JSpinner JSpinners}.
     */
	@Override
	public void addActionListeners(ActionListener actionListener, FocusAdapter focusAdapter, KeyAdapter keyAdapter, ChangeListener changeListener)
	{
		cbxKey1.addActionListener(actionListener);
		cbxKey2.addActionListener(actionListener);
	}
	
    /**
     * Gets the {@link settings.Action.Action#KEYSTROKE Action.KEYSTROKE} {@link settings.Command} arguments currently displayed.
     * @return The {@link settings.Action.Action#KEYSTROKE Action.KEYSTROKE} {@link settings.Command} arguments currently displayed.
     */
	@Override
	public List<String> getArguments() 
	{ 
		List<String> arguments = new ArrayList<String>();
		
		if (cbxKey1.getSelectedIndex() != -1)
		{
			arguments.add((String) cbxKey1.getSelectedItem());
		}
		if (cbxKey2.getSelectedIndex() != -1)
		{
			arguments.add((String) cbxKey2.getSelectedItem());
		}
		
		return arguments;
	}
	
    /**
     * <ul>
     *     <li>Sets the {@link settings.Action.Action#KEYSTROKE Action.KEYSTROKE} {@link settings.Command Command} arguments.</li>
     *     <li>Updates the UI based on the given arguments.</li>
     * </ul>
     * @param arguments The {@link settings.Action.Action#KEYSTROKE Action.KEYSTROKE} {@link settings.Command Command} arguments used to update the UI.
     */
	@Override
	public void setArguments(List<String> arguments) 
	{
		if (arguments != null)
		{
			if (arguments.size() > 0 && arguments.get(0) != null) 
			{
				cbxKey1.setSelectedItem(arguments.get(0));
			}
			else
			{
				cbxKey1.setSelectedIndex(-1);
			}
			
			if (arguments.size() > 1 && arguments.get(1) != null) 
			{
				cbxKey2.setSelectedItem(arguments.get(1));
			}
			else
			{
				cbxKey2.setSelectedIndex(-1);
			}
		}
		else
		{
			cbxKey1.setSelectedIndex(-1);
			cbxKey2.setSelectedIndex(-1);
		}
	}
	
	/**
	 * Gets a collection of accepted {@link settings.Action.Action#KEYSTROKE KEYSTROKE} {@link settings.Command Command} arguments.
	 * @return A collection of accepted {@link settings.Action.Action#KEYSTROKE KEYSTROKE} {@link settings.Command Command} arguments.
	 */
	private String[] getKeyCommands()
	{
		return new String[] { "", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
							  "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
							  "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8",
							  "9", "TAB", "SHIFT", "CONTROL", "ALT", "SPACE", "ENTER", "UP",
							  "DOWN", "LEFT", "RIGHT", "F1", "F2", "F3", "F4", "F5", "F6",
							  "F7", "F8", "F9" };
	}
}
